##################################################################################################################################
#
# Copyright (C) 2008-2013 Mind System Connection. Lda. Part of Daidze Software, created by Siqsuruq. All other rights reserved.
#
# Mind System Connection Webpage: www.msysc.org
# Daidze Software Page: www.daidze.com www.daidze.org
# E-mail: admin@msysc.org, siqsuruq@gmail.com
#
##################################################################################################################################

proc add_payment_type {} {
	set values [$::daidze_main get_values]
	set msg_list [dict create name "Payment type name is obligatory."]
	set flags [list]

	foreach key_val [dict keys $msg_list] {
		if {[dict get $values $key_val] == ""} {
			tk_messageBox -icon error -type ok -message "[::msgcat::mc "[dict get $msg_list $key_val]"]"
			lappend flags 0
		} else { lappend flags 1}
	} 

	if {[lsearch -not -exact $flags 1] == "-1"} {
		if {[dict get $values def] eq "1"} {
			if {[$::db select_all payment_type * "def=\'1\'"] != ""} {
				tk_messageBox -icon info -type ok -message "[::msgcat::mc "There is already default payment type. This one will be stored as generic type."]"
				dict set values def 0
			}
		}
		
		set def_payment [dict get $values def_payment]
		set values [dict remove $values def_payment]
		
		$::db insert_all payment_type $values
		
		if {$def_payment != 0} {
			write_config_db daidze [set d [dict create workstation [dict create default_payment_id [$::db select_last_inserted payment_type id]]]]
		} else {
			write_config_db daidze [set d [dict create workstation [dict create default_payment_id 0]]]
		}
		read_config_db [file join conf $::default_config_db]

		$::daidze_main load_main payment_type payment_type.xml $::main 1
	}
}

proc edit_payment_type {table_var} {
	set sdata [dict create]
	set my_tablelist [dict get [$::daidze_main get_fdata] $table_var]
	set vars {id name}
	if {[$my_tablelist curselection] ne ""} {
		set row [$my_tablelist rowcget [$my_tablelist curselection] -text]
		foreach key $vars value $row {
			dict set sdata $key $value
		}
		dict set sdata def [$::db select_all payment_type def "name=\'[dict get $sdata name]\'" list]
		dict set sdata terms [lindex [$::db select_all payment_type terms "name=\'[dict get $sdata name]\'" list] 0]
		$::daidze_main load_main payment_type edit_payment_type.xml $::main
		$::daidze_main update_form $sdata
	} else {
		tk_messageBox -icon info -type ok -message "[::msgcat::mc "Select entry to edit."]"
	}
}

proc update_payment_type {} {
	puts [set values [$::daidze_main get_values]]

	set msg_list [dict create name "Payment type name is obligatory."]
	set flags [list]

	foreach key_val [dict keys $msg_list] {
		if {[dict get $values $key_val] == ""} {
			tk_messageBox -icon error -type ok -message "[::msgcat::mc "[dict get $msg_list $key_val]"]"
			lappend flags 0
		} else { lappend flags 1}
	} 

	if {[lsearch -not -exact $flags 1] == "-1"} {
		if {[dict get $values def] eq "1"} {
			if {[$::db select_all payment_type * "def=\'1\'"] != ""} {
				tk_messageBox -icon info -type ok -message "[::msgcat::mc "There is already default payment type. This one will be stored as generic type."]"
				dict set values def 0
			}
		}
		
		set def_payment [dict get $values def_payment]
		set values [dict remove $values def_payment]
		$::db update_all payment_type $values
		
		if {$def_payment != 0} {
			write_config_db daidze [set d [dict create workstation [dict create default_payment_id [dict get $values id]]]]
		} else {
			write_config_db daidze [set d [dict create workstation [dict create default_payment_id 0]]]
		}
		read_config_db [file join conf $::default_config_db]
		$::daidze_main load_main payment_type payment_type.xml $::main 1
	}
}
